# coding=utf-8

import logging


class Config:
    PROP_APP_NAME = "app.name"
    PROP_LOG_DIR = "log.dir"
    PROP_LOGNAME = "log.filename"
    
    ConfigFile = "dailyCheck.properties"
    prop = {}
    errorRe = b'(Can\'t connect to MySQL server)|(record client number=)'

    mailto_list = ["13810184969@139.com"]
    mail_host = "smtp.tom.com"  # service
    mail_user = "liuymapp"  # username
    mail_pass = "123liuymapp"  # passwork
    mail_postfix = "tom.com"
    mail_from = mail_user + "@" + mail_postfix;

    def setConfigFile(self, fileName):
        self.ConfigFile = fileName

    def getLogDir(self):
        return self.prop[self.PROP_LOG_DIR]

    def getLogName(self):
        return self.prop[self.PROP_LOGNAME]

    def getAppName(self):
        return self.prop[self.PROP_APP_NAME]
    
    def getLogErrorRe(self):
        return self.errorRe
            
    def display(self):

        print("init ", self.ConfigFile)
        try:
            propFile = open(self.ConfigFile, "r")
            for line in propFile:
                if line.find('=') > 0 and not line.startswith('#'):
                    proText = line.replace('\n', '').split('=')
                    self.prop[proText[0]] = proText[1]
                    # print(proText[0], "=", proText[1])
            propFile.close()
            if self.prop.get(self.PROP_LOG_DIR) == None:
                raise ValueError('please set ' + self.PROP_LOG_DIR)
            if self.prop.get(self.PROP_LOGNAME) == None:
                raise  ValueError('please set ' + self.PROP_LOGNAME)
        except Exception as e:
            logging.error(e)
        else:
            return True
